<?php

class memberDAL {
	private $dbConnection = "";

	public function __construct($dbConnection) {
		$this -> dbConnection = $dbConnection;
	}

	// Save member in database. 
	public function AddMember(Member $member) {
		/*$sql = "INSERT INTO `user`(
					`firstName`, 
					`lastName`, 
					`userName`, 
					`password`, 
					`mail`, 
					`socialSecurityNumber`) 
					VALUES (
					?,
					?,
					?,
					?,
					?,
					?)";
		$stmt = $this->dbConnection -> Prepare($sql);
		$stmt -> bind_param("ssssss", $member -> firstName, $member -> lastName, $member -> userName, $member -> password, $member -> mail, $member -> ssn);
		
		if ($stmt -> execute()) {
			$stmt -> close();
			return true;
		} else {
			$stmt -> close();
			return false;
		}*/
	}
	
	//Check if the username is busy
	public function UserNameExists($userName) {
		$sql = "SELECT `userName` FROM `user` WHERE `userName`= ?";
		$stmt = $this -> dbConnection -> Prepare($sql);

		$stmt -> bind_param("s", $userName);
		$result = $stmt->bind_result($userName);
		$user = null;
			while ($stmt->fetch()) { 
				$user = $userName;
			}

		if ($user != null) {
			return true;
			$stmt -> close();
		}
		$stmt -> close();
		return false;
	}
	
	//Check if the username is busy
	public function SsnExists($ssn) {
		$sql = "SELECT `socialSecurityNumber` FROM `user` WHERE  `socialSecurityNumber` = ?";

		$stmt = $this -> dbConnection -> Prepare($sql);

		$stmt -> bind_param("s", $ssn);
		$stmt -> execute();
		$result = $stmt->bind_result($ssn);
		$oldSsn = null;
			while ($stmt->fetch()) { 
				$oldSsn = $ssn;
			}

		if ($oldSsn != null) {
			return true;
			$stmt -> close();
		}
		$stmt -> close();
		return false;
	}
	
	//Deletes member where the username and password match
	public function DeleteMember($userName, $password) {
		$sql = "DELETE FROM `user` WHERE `userName`=? AND `password` =?";
		$stmt = $this -> dbConnection -> Prepare($sql);
		$stmt -> bind_param("ss", $userName, $password);

		if ($stmt -> execute()) {
			$stmt -> close();
			return true;
		} else {
			$stmt -> close();
			return false;
		}
	}
	
	//Confirms that the person logging in to our page is actually a member. 
	public function ConfirmUser($userName, $password) {
		$sql = "SELECT `userId` FROM `user` WHERE  `userName`= ? AND `password`= ?";
		$stmt = $this -> dbConnection -> Prepare($sql);
		$stmt -> bind_param("ss", $userName, $password);
		$stmt->execute();
		$result = $stmt->bind_result($userId);
		
		$user = null;
			while ($stmt->fetch()) { 
				$user = $userId;
			}

		if ($user != null) {
			return true;
			$stmt -> close();
		}
		$stmt -> close();
		return false;
	}

	public function TestMemberDAL() {

	}

}
